package ui.component
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	import globals.Global;

	import util.ColorUtil;
	import util.TextStyle;

	/**
	 * 复选按钮
	 * @author luguobin
	 */
	public class CheckBox extends Sprite
	{
		protected var _id:int;
		protected var _enable:Boolean = true;
		protected var _selected:Boolean;
		protected var _tick:Bitmap;
		protected var _selectedBg:Bitmap;
		protected var _textFiled:TextField;
		protected var _textWidth:Number = 120;
		protected var _horizontalGap:Number = 0; //水平间隔
		protected var _text:String;
		protected var _htmlText:String;
		protected var _data:Object;

		//protected var _clickCallback:Function;
		public function CheckBox()
		{
			super();
			init();
		}

		protected function init():void
		{
			_selectedBg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("checkbox_bg"));
			this.addChild(_selectedBg);
			_tick = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("checkbox_select"));
			_tick.visible = false;
			this.addChild(_tick);
			this.addEventListener(MouseEvent.CLICK, clickHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		}

		protected function changeToggle():void
		{
			if(_selected)
			{
				_selected = false;
				_tick.visible = false;
			}
			else
			{
				_selected = true;
				_tick.visible = true;
			}
			this.dispatchEvent(new Event(Event.CHANGE));
		}

		private function initTextField(isHtmlText:Boolean = false):void
		{
			if(_textFiled == null)
			{
				_textFiled = new TextField();
				_textFiled.autoSize = TextFieldAutoSize.LEFT;
				_textFiled.selectable = false;
				_textFiled.mouseWheelEnabled = false;
				_textFiled.mouseEnabled = false;
				//_textFiled.textColor = 0xFFFFFF;
				_textFiled.defaultTextFormat = TextStyle.getTextFormat();
				this.addChild(_textFiled);
			}

			if(isHtmlText)
			{
				_textFiled.wordWrap = _textFiled.multiline = true;
				_textFiled.htmlText = _htmlText;
				_textFiled.width = _textWidth;
				_textFiled.x = _selectedBg.width + _horizontalGap;
				_textFiled.y = -2;
			}
			else
			{
				_textFiled.wordWrap = _textFiled.multiline = false;
				_textFiled.text = _text;
				_textFiled.x = _selectedBg.width + _horizontalGap;
				_textFiled.y = (_selectedBg.height - _textFiled.textHeight) / 2 - 1;
			}
		}

		private function clickHandler(event:MouseEvent):void
		{
			if(_enable)
			{
				changeToggle();
			}
		}

		private function mouseOverHandler(event:MouseEvent):void
		{
			if(_enable)
			{
				ColorUtil.highLight(this);
			}
		}

		private function mouseOutHandler(event:MouseEvent):void
		{
			if(_enable)
			{
				ColorUtil.darkLight(this);
			}
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get id():int
		{
			return _id;
		}

		public function set enable(value:Boolean):void
		{
			_enable = value;

			if(!value)
			{
				ColorUtil.setBlackColor(this);
				_textFiled.textColor = 0xababab;
			}
		}

		public function get enable():Boolean
		{
			return _enable;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			_tick.visible = value;
		}

		public function get text():String
		{
			return _text;
		}

		public function set text(value:String):void
		{
			_text = value;
			initTextField();
		}

		public function get htmlText():String
		{
			return _htmlText;
		}

		public function set htmlText(value:String):void
		{
			_htmlText = value;
			initTextField(true);
		}

		public function set textWidth(value:Number):void
		{
			_textWidth = value;
		}

		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}

		/**文字与选择框的水平距离大小（以像素为单位）**/
		public function set horizontalGap(value:Number):void
		{
			_horizontalGap = value;

			if(_textFiled != null)
			{
				_textFiled.x = _selectedBg.width + _horizontalGap;
			}
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}
	}
}